﻿using Caliburn.Micro;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.IOC.Core.Mediators;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation;
using SlamDunk.MES.Presentation.Models.NotifyModels;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;

namespace SlamDunk.MES.Client.ViewModels.StockInOrder
{
    /// <summary>
    /// 入库单据打印条码ViewModel
    /// </summary>
    public class PrintUniqueBarcodeViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 库存单据服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IStockOrderContract> _iStockOrderContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public PrintUniqueBarcodeViewModel(ServiceProxy<IStockOrderContract> iStockOrderContract, IWindowManager windowManager)
        {
            this._iStockOrderContract = iStockOrderContract;
            this._windowManager = windowManager;
        }

        #endregion

        #region 属性
        #region 入库明细列表
        /// <summary>
        /// 条码打印列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<StockInOrderDetailNm> StockInOrderDetails { get; set; }
        #endregion

        #region 入库属性
        /// <summary>
        /// 入库属性
        /// </summary>
        [DependencyProperty]
        public StockInOrderInfo _StockInOrderInfo { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(StockInOrderInfo stockInOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(StockInOrderInfo stockInOrderInfo)
        {
            this._StockInOrderInfo = stockInOrderInfo;
            await ReloadStockInOrders();
        }
        #endregion

        #region 打印/补打条码 —— async void PrintBarcodeNos()
        /// <summary>
        ///  打印/补打条码
        /// </summary>
        public async void PrintBarcodeNos(StockInOrderDetailNm stockInOrderDetailNm)
        {
            if (stockInOrderDetailNm.HasPrintedBarcode == true)
            {
                if (stockInOrderDetailNm.BarcodeModeType)//判断是否唯一码
                {
                    //弹出设置通用码打印数量页面
                    ReprintUniqueBarcodeViewModel viewModel = ResolveMediator.Resolve<ReprintUniqueBarcodeViewModel>();
                    viewModel.Load(stockInOrderDetailNm);
                    await this._windowManager.ShowDialogAsync(viewModel);
                }
                else
                {
                    //弹出设置通用码打印数量页面
                    PrintGeneralBarcodeViewModel viewModel = ResolveMediator.Resolve<PrintGeneralBarcodeViewModel>();
                    viewModel.Load(stockInOrderDetailNm);
                    bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                }
            }
            else
            {
                if (stockInOrderDetailNm.BarcodeModeType)//判断是否唯一码
                {
                    foreach (string barcodeNo in stockInOrderDetailNm.RelatedBarcodeNos)
                    {
                        //打印条码打码
                        string BarcodeName = stockInOrderDetailNm.BarcodeModeType ? "唯一码" : "通用码";
                        string str = $@"^XA
^CI28
^CW1,E:SIMSUN.FNT
^PQ1,0,0.5,Y
^FTT
^FO60,60^BQN,4,11^FDLA,{barcodeNo}^FS
^FO60,340^A1N,36,36^FD条码号：{barcodeNo}^FS
^FO60,20^A1,36,36^FD{stockInOrderDetailNm.MaterialName}^FS
^FO295,75^A1,30,30^FD规格：{stockInOrderDetailNm.Specification}^FS
^FO295,110^A1,30,30^FD批号：{stockInOrderDetailNm.ProductionBatchNo}^FS
^FO295,150^A1,30,30^FD生产日期：{stockInOrderDetailNm.ProducedDate}^FS
^FO295,190^A1,30,30^FD接收日期：{stockInOrderDetailNm.ReceptionDate}^FS
^FO295,210^A1,30,30^条码类型：{BarcodeName}^FS
^FO295,240^A1,30,30^FD打印日期：{DateTime.Now:yyyy-MM-dd}^FS
^XZ
";
                        string printName = ConfigurationManager.AppSettings[Constants.PrintNameKey];
                        //PrinterMediator.SendInstructionsToPrinter(printName, str);
                        //TODO 打印
                    }
                }
                else
                {
                    //弹出设置通用码打印数量页面
                    PrintGeneralBarcodeViewModel viewModel = ResolveMediator.Resolve<PrintGeneralBarcodeViewModel>();
                    viewModel.Load(stockInOrderDetailNm);
                    bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                }
                await Task.Run(() => this._iStockOrderContract.Channel.PrintStockOrderBarcode(this._StockInOrderInfo.Id, stockInOrderDetailNm.StockOrderDetailId.Value));
            }
            this.Busy();
            await ReloadStockInOrders();
            this.Idle();
        }
        #endregion

        //Private
        #region 加载入库单明细列表 —— async Task ReloadStockInOrders()
        /// <summary>
        /// 加载入库单明细列表
        /// </summary>
        private async Task ReloadStockInOrders()
        {
            this.Busy();
            IEnumerable<StockOrderDetailInfo> StockOrderDetailInfos = await Task.Run(() => this._iStockOrderContract.Channel.GetStockOrderDetails(this._StockInOrderInfo.Id));
            StockInOrderDetails = new ObservableCollection<StockInOrderDetailNm>();
            foreach (StockOrderDetailInfo stockOrderDetailInfo in StockOrderDetailInfos)
            {
                if (stockOrderDetailInfo.MaterialInfo.MaterialBarcodeSettingInfo.NeedPrintBarcode)
                {
                    if (stockOrderDetailInfo.MaterialInfo.MaterialBarcodeSettingInfo.BarcodeMode == BarcodeMode.General)
                    {
                        List<string> RelatedBarcodeNos = new List<string>();
                        RelatedBarcodeNos.Add(stockOrderDetailInfo.MaterialInfo.Number);
                        StockInOrderDetails.Add(new StockInOrderDetailNm(
                            stockOrderDetailInfo.MaterialId,//物料定义Id
                            stockOrderDetailInfo.MaterialInfo.Name,//物料名称
                            stockOrderDetailInfo.MaterialInfo.Number,//物料编号
                            stockOrderDetailInfo.MaterialLotId,//物料批次Id
                            stockOrderDetailInfo.Quantity,//数量
                            stockOrderDetailInfo.Sort,//排序
                            stockOrderDetailInfo.Description,//描述
                            RelatedBarcodeNos,//库存单据条码编号集
                            stockOrderDetailInfo.MaterialLotInfo?.SupplierId,//供应商Id
                            stockOrderDetailInfo.MaterialLotInfo?.SupplierName,//供应商名称
                            stockOrderDetailInfo.MaterialLotInfo?.ProductionBatchNo,//生产批号
                            stockOrderDetailInfo.MaterialLotInfo?.ProducedDate,//生产日期
                            stockOrderDetailInfo.MaterialLotInfo?.GuaranteePeriod,//保质期
                            stockOrderDetailInfo.MaterialLotInfo?.ExpiredDate,//过期日期
                            stockOrderDetailInfo.MaterialLotInfo?.ReceptionDate,//接收日期
                            stockOrderDetailInfo.MaterialLotInfo?.Specification,//规格
                            stockOrderDetailInfo.MaterialLotInfo?.MaterialSourceType,//物料来源
                            stockOrderDetailInfo.HasPrintedBarcodes == true ? "已打印" : "未打印",//是否已打印名称
                            "通用码",//是否唯一码名称
                            false,//是否已打印
                            false,//是否唯一码
                            stockOrderDetailInfo.HasPrintedBarcodes == true ? "补打" : "打印",//操作按钮名称
                            stockOrderDetailInfo.Id,//单据明细Id
                            stockOrderDetailInfo.MaterialInfo.StandardUnitNo
                              ));
                    }
                    if (stockOrderDetailInfo.MaterialInfo.MaterialBarcodeSettingInfo.BarcodeMode == BarcodeMode.Unique)
                    {
                        StockInOrderDetails.Add(new StockInOrderDetailNm(
                        stockOrderDetailInfo.MaterialId,//物料定义Id
                        stockOrderDetailInfo.MaterialInfo.Name,//物料名称
                        stockOrderDetailInfo.MaterialInfo.Number,//物料编号
                        stockOrderDetailInfo.MaterialLotId,//物料批次Id
                        stockOrderDetailInfo.Quantity,//数量
                        stockOrderDetailInfo.Sort,//排序
                        stockOrderDetailInfo.Description,//描述
                        new List<string>(stockOrderDetailInfo.RelatedBarcodeNos),//库存单据条码编号集
                        stockOrderDetailInfo.MaterialLotInfo?.SupplierId,//供应商Id
                        stockOrderDetailInfo.MaterialLotInfo?.SupplierName,//供应商名称
                        stockOrderDetailInfo.MaterialLotInfo?.ProductionBatchNo,//生产批号
                        stockOrderDetailInfo.MaterialLotInfo?.ProducedDate,//生产日期
                        stockOrderDetailInfo.MaterialLotInfo?.GuaranteePeriod,//保质期
                        stockOrderDetailInfo.MaterialLotInfo?.ExpiredDate,//过期日期
                        stockOrderDetailInfo.MaterialLotInfo?.ReceptionDate,//接收日期
                        stockOrderDetailInfo.MaterialLotInfo?.Specification,//规格
                        stockOrderDetailInfo.MaterialLotInfo?.MaterialSourceType,//物料来源
                        stockOrderDetailInfo.HasPrintedBarcodes == true ? "已打印" : "未打印",//是否已打印名称
                        "唯一码",//是否唯一码名称
                        stockOrderDetailInfo.HasPrintedBarcodes,//是否已打印
                        true,//是否唯一码
                        stockOrderDetailInfo.HasPrintedBarcodes == true ? "补打" : "打印",//操作按钮名称
                        stockOrderDetailInfo.Id,//单据明细Id
                        stockOrderDetailInfo.MaterialInfo.StandardUnitNo
                        ));
                    }
                }
            }
            this.Idle();
        }
        #endregion
        #endregion
    }
}
